package com.wj.activiti;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.Execution;
import org.activiti.engine.runtime.ProcessInstance;

import java.util.List;

public class SignaleEventReceived {

    public static void main(String[] args) {
        // 创建activiti配置对象
        ProcessEngineConfiguration processEngineConfiguration = ProcessEngineConfiguration
                .createProcessEngineConfigurationFromResource("my-config.xml");

        // 创建流程殷勤
        ProcessEngine engine = processEngineConfiguration.buildProcessEngine();

        // 得到流程存储服务组件
        RepositoryService repositoryService = engine.getRepositoryService();

        List<Deployment> deployments = repositoryService.createDeploymentQuery().list();
        for(Deployment deployment : deployments) {
            repositoryService.deleteDeployment(deployment.getId(), true);
        }

//        // 得到运行服务组件
//        RuntimeService runtimeService = engine.getRuntimeService();
//
//        // 部署流程描述文件
//        Deployment dep = repositoryService.createDeployment()
//                .addClasspathResource("bpmn/signalEventReceived.bpmn").deploy();
//
//        // 获取流程定义
//        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
//                .deploymentId(dep.getId()).singleResult();
//
//        // 启动流程
//        ProcessInstance processInstance = runtimeService.startProcessInstanceById(processDefinition.getId());
//
//        // 查询执行流
//        Execution execution = runtimeService.createExecutionQuery().activityId("usertask1").singleResult();
//
//        System.out.println("当前节点：" + execution.getActivityId());
//
//        // 触发receiveTask
//        runtimeService.trigger(execution.getId());
//
//        //查询当前节点
//        execution = runtimeService.createExecutionQuery().activityId("signalCatchEvent").singleResult();
//
//        System.out.println("调用 trigger 方法后当前节点 :" + execution.getActivityId());
//
//        //发送信号给事件，流程结束
//        runtimeService.signalEventReceived("testSignal");
//        List exes = runtimeService .createExecutionQuery().processInstanceId(processInstance.getId()).list();
//        System.out.println ("触发 Catching 事件后，执行流数量 :" + exes.size());
    }
}
